package com.ruoyi.wlxz.util;

import com.ruoyi.common.utils.DateUtils;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import static org.quartz.JobBuilder.newJob;

import static org.quartz.TriggerBuilder.newTrigger;

@Component
public class QuartzSchedulerActivity {
    @Autowired
    private Scheduler scheduler;

    /**
     * 启动定时任务,报名中
     */
    public void start(String datatime ,String ActivityName) {
        JobDetail job = newJob(QuartzActivityJob.class)
                .withIdentity("job-报名中-"+ActivityName, "group-报名中-"+ActivityName)
                .usingJobData("type", "报名中")
                .usingJobData("ActivityName", ActivityName)
                .build();
        Trigger trigger = newTrigger()
                .withIdentity("trigger-报名中-"+ActivityName, "group-报名中-"+ActivityName)
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule(datatime))//根据cron表达式
                .build();

        try {
            scheduler.scheduleJob(job, trigger);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
        try {
            scheduler.start();
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }/**
     * 启动定时任务,等待中
     */
    public void wait(String datatime,String ActivityName) {
        JobDetail job = newJob(QuartzActivityJob.class)
                .withIdentity("job-等待中-"+ActivityName, "group-等待中-"+ActivityName)
                .usingJobData("type", "等待中")
                .usingJobData("ActivityName", ActivityName)
                .build();
        Trigger trigger = newTrigger()
                .withIdentity("trigger-等待中-"+ActivityName, "group-等待中-"+ActivityName)
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule(datatime))
                .build();

        try {
            scheduler.scheduleJob(job, trigger);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
        try {
            scheduler.start();
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }
    
    
    /**
     * 启动定时任务,进行中
     */
    public void ongoing(String datatime,String ActivityName) {
        JobDetail job = newJob(QuartzActivityJob.class)
                .withIdentity("job-进行中-"+ActivityName, "group-进行中-"+ActivityName)
                .usingJobData("type", "进行中")
                .usingJobData("ActivityName", ActivityName)
                .build();
        Trigger trigger = newTrigger()
                .withIdentity("trigger-进行中-"+ActivityName, "group-进行中-"+ActivityName)
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule(datatime))
                .build();

        try {
            scheduler.scheduleJob(job, trigger);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
        try {
            scheduler.start();
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 启动定时任务,已结束
     */
    public void end(String datatime,String ActivityName) {
        JobDetail job = newJob(QuartzActivityJob.class)
                .withIdentity("job-已结束-"+ActivityName, "group-已结束-"+ActivityName)
                .usingJobData("type", "已结束")
                .usingJobData("ActivityName", ActivityName)
                .build();
        Trigger trigger = newTrigger()
                .withIdentity("trigger-已结束-"+ActivityName, "group-已结束-"+ActivityName)
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule(datatime))
                .build();

        try {
            scheduler.scheduleJob(job, trigger);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
        try {
            scheduler.start();
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }
}